package tree;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root){
        if(root == null) {
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrder(TreeNode root){
        if(root == null) {
            return ;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    // 获取树中节点的个数
    int size(TreeNode root){
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    //  获取叶子节点的个数
    int getLeafNodeCount(TreeNode root){
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left)
                + getLeafNodeCount(root.right);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k - 1)
                + getKLevelNodeCount(root.right,k - 1);
    }
    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if(root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return left > right ? left + 1 : right + 1;
    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val){
        if(root == null) {
            return null;
        }
        //到这里确定不是空
        if(root.val == val) {
            return root;
        }
        //判断val是否相同
        TreeNode left = find(root.left,val);
        if(left != null) {
            return left;
        }
        TreeNode right = find(root.right,val);
        if(right != null) {
            return right;
        }
        return null;
    }

    /**
     * 1. 检查两颗树是否相同。
     * https://leetcode.cn/problems/same-tree/
     * @param p
     * @param q
     * @return
     */
        public boolean isSameTree(TreeNode p, TreeNode q) {
            //判断是否单方为空 如果是 就返回false
            if((p == null && q != null) || (p != null && q == null)) {
                return false;
            }
            //同时为null 就代表当前是一样的
            if(p == null && q ==null) {
              return true;
            }
            //现在双方都不为空 判断val 值 如果不一样返回false
            if(p.val != q.val) {
                return false;
            }
            //开始递归 只要有一边返回是 false 值就是false
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }

    /**
     * 另一颗树的子树。
     * https://leetcode.cn/problems/subtree-of-another-tree/
     * @param root
     * @param subRoot
     * @return
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null || subRoot == null ) {
            return false;
        }
        if(isSameTree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left,subRoot)) {
            return true;
        }
        if(isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;
    }
    /**
     * 3. 翻转二叉树。
     * https://leetcode-cn.com/problems/invert-binary-tree/
     */
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
